package com.bizfx.component.fields
{
	import flash.events.Event;
	
	import com.bizfx.component.views.IDetailView;
	import com.bizfx.utils.StringUtils;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.controls.DataGrid;
	import mx.events.FlexEvent;
	import mx.utils.UIDUtil;
	
	public class DataGridField extends DataGrid implements IField
	{
		public function DataGridField()
		{
			super();
			this.setStyle("disabledColor", 0x000000);
		}
		[Bindable]
		public function get fieldKey():String
		{
			return this.id;
		}
		
		public function set fieldKey(value:String):void
		{
			this.id = value;
		}
		
		private var _bindProperty:String;
		[Bindable]
		public function get bindProperty():String
		{
			return _bindProperty;
		}
		
		public function set bindProperty(value:String):void
		{
			this._bindProperty = value;
		}

		[Bindable]
		override public function get data():Object
		{
			if (!this.dataProvider){
				this.dataProvider = new ArrayCollection();
			}
			return this.dataProvider;
		}
		
		override public function set data(value:Object):void {
			this.dataProvider = value;
		}
				
//		private var _view:IDetailView;
//
//		public function get view():IDetailView
//		{
//			return this._view;
//		}
//		
//		public function set view(value:IDetailView):void {
//			if (StringUtils.isNullOrEmpty(this.id)) {
//				this.id = UIDUtil.createUID();
//			}
//			if (this._view !== value) {
//				if (_view) {
//					_view.removeFieldByKey(this.fieldKey);
//				}
//				this._view = value;
//				if (_view) {
//					_view.addField(this);
//				}
//			}
//		}
		
		private var _idField:String;
		[Bindable]
		public function get idField():String {
			return this._idField;
		}
		
		public function set idField(value:String):void {
			this._idField = value;
		}
		
		[Inspectable(category="General", enumeration="true,false", defaultValue="false")]
		[Bindable("readOnlyChanged")]
		public function get readOnly():Boolean {
			return this.editable;
		}
		
		public function set readOnly(value:Boolean):void {
			if (this.editable == value) {
				this.editable = !value;
				this.dispatchEvent(new Event("readOnlyChanged"));
			}
		}
		
		public function containListItem(item:Object):Boolean {
			var i:int = 0;
			if (this.dataProvider && this.dataProvider is IList) {
				var dataList:IList = this.dataProvider as IList;
				if (!StringUtils.isNullOrEmpty(this._idField)) {
					if (item.hasOwnProperty(this.idField)) {
						var itemId:String = item[this.idField];
						for (i = 0; i < dataList.length; i++) {
							var data:Object = dataList.getItemAt(i);
							if (data.hasOwnProperty(this.idField)){
								var id:String = data[this.idField].toString();
								if (itemId == id) {
									return true;
								}
							}
						}	
					}
				} 
				
				for (i = 0; i < dataList.length; i++) {
					if (item == dataList.getItemAt(i)) {
						return true;
					}
				}
			}
			return false;
		}
	}
}